/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package calcmvc;

import java.lang.Double;
import java.util.ArrayList;

/**
 *
 * @author Administrator
 */
public class CalcController {
    
    private CalcModel calculator;
    private CalcViewer calcGUI;
    private String currentCalcInput;
    private boolean lastButtonPressedWasANumber = false;
    
    public CalcController()
    {
        calculator = new CalcModel(this);
        calcGUI = new CalcViewer(this);
        currentCalcInput = "";
    }
    
    public void divideButtonPressed()
    {
        if( lastButtonPressedWasANumber )
        {
            calcGUI.addStringToTextArea("/");
            lastButtonPressedWasANumber = false;
            
            calculator.addOperation("/");
            if(currentCalcInput != "")
            {
                calculator.addNumber(Double.valueOf(currentCalcInput));
                currentCalcInput = "";
            }
        }
    }
    
    public void multiplyButtonPressed()
    {
        if( lastButtonPressedWasANumber )
        {
            calcGUI.addStringToTextArea("*");
            lastButtonPressedWasANumber = false;
            
            calculator.addOperation("*");
            if(currentCalcInput != "")
            {
                calculator.addNumber(Double.valueOf(currentCalcInput));
                currentCalcInput = "";
            }
        }
    }
    
    public void minusButtonPressed()
    {
        if( lastButtonPressedWasANumber )
        {
            calcGUI.addStringToTextArea("-");
            lastButtonPressedWasANumber = false;
            
            calculator.addOperation("-");
            if(currentCalcInput != "")
            {
                calculator.addNumber(Double.valueOf(currentCalcInput));
                currentCalcInput = "";
            }
        }
    }
    
    public void sevenButtonPressed()
    {
        currentCalcInput += "7";
        calcGUI.addStringToTextArea("7");
        lastButtonPressedWasANumber = true;
        
        
    }
    
    public void eightButtonPressed()
    {
        currentCalcInput += "8";
        calcGUI.addStringToTextArea("8");
        lastButtonPressedWasANumber = true;
        
    }
    
    public void nineButtonPressed()
    {
        currentCalcInput += "9";
        calcGUI.addStringToTextArea("9");
        lastButtonPressedWasANumber = true;
    }
    
    public void plusButtonPressed()
    {
        if( lastButtonPressedWasANumber )
        {
            calcGUI.addStringToTextArea("+");
            lastButtonPressedWasANumber = false;
            
            calculator.addOperation("+");
            if(currentCalcInput != "")
            {
                calculator.addNumber(Double.valueOf(currentCalcInput));
                currentCalcInput = "";
            }
        }
    }
    
    public void fourButtonPressed()
    {
        currentCalcInput += "4";
        calcGUI.addStringToTextArea("4");
        lastButtonPressedWasANumber = true;
    }
    
    public void fiveButtonPressed()
    {
        currentCalcInput += "5";
        calcGUI.addStringToTextArea("5");
        lastButtonPressedWasANumber = true;
    }
    
    public void sixButtonPressed()
    {
        currentCalcInput += "6";
        calcGUI.addStringToTextArea("6");
        lastButtonPressedWasANumber = true;
    }
    
    public void oneButtonPressed()
    {
        currentCalcInput += "1";
        calcGUI.addStringToTextArea("1");
        lastButtonPressedWasANumber = true;
    }
    
    public void twoButtonPressed()
    {
        currentCalcInput += "2";
        calcGUI.addStringToTextArea("2");
        lastButtonPressedWasANumber = true;
    }
    
    public void threeButtonPressed()
    {
        currentCalcInput += "3";
        calcGUI.addStringToTextArea("3");
        lastButtonPressedWasANumber = true;
    }
    
    public void equalsButtonPressed()
    {
        if(currentCalcInput != "")
            {
                calculator.addNumber(Double.valueOf(currentCalcInput));
                currentCalcInput = "";
            }
        
        calcGUI.getTextArea().setText(Double.toString(calculator.calculateTotal()));
        currentCalcInput = "";
        lastButtonPressedWasANumber = true;
    }
    
    public void zeroButtonPressed()
    {
        currentCalcInput += "0";
        calcGUI.addStringToTextArea("0");
        lastButtonPressedWasANumber = true;
    }
    
    public void commaButtonPressed()
    {
        if( lastButtonPressedWasANumber )
        {
            calcGUI.addStringToTextArea(",");
            lastButtonPressedWasANumber = false;
        }
    }
    public void powerButtonPressed()
    {
        if( lastButtonPressedWasANumber )
        {
            calcGUI.addStringToTextArea("^");
            lastButtonPressedWasANumber = false;
            
            calculator.addOperation("^");
            if(currentCalcInput != "")
            {
                calculator.addNumber(Double.valueOf(currentCalcInput));
                currentCalcInput = "";
            }
        }
    }
    
    public void clearButtonPressed()
    {
        currentCalcInput = "";
        calcGUI.getTextArea().setText(null);
        calculator.clear();

    }
    
    public void saveButtonPressed()
    {
        if(!currentCalcInput.equals(""))
            {
                calculator.addNumber(Double.valueOf(currentCalcInput));
            }
        
        lastButtonPressedWasANumber = true;
        ArrayList<ArrayList> al = new ArrayList<ArrayList>();
        
        al.add(calculator.getNumbersInCalc());
        al.add(calculator.getOperationsInCalc());
        
        calculator.setSavedCalculation(al);
        //calculator.save();
    }
    
    public void clearSavedCalculationAndSaveCalculationButtonPressed()
    {
        calculator.deleteSavedCalculation();
        saveButtonPressed();
    }
    
    public void clearSavedCalculationButtonPressed()
    {
        calculator.deleteSavedCalculation();
    }
    
    public void loadButtonPressed()
    {
        if(calculator.hasSavedCalculation())
        {
        
            ArrayList<ArrayList> al = new ArrayList<ArrayList>();
            al = calculator.getSavedCalculation();
            ArrayList<Double> numbers = al.get(0);
            ArrayList<String> operations = al.get(1);
            calculator.load();
        
            calcGUI.getTextArea().setText(null);
            currentCalcInput = "";
        
            for(int i = 0; i < al.get(0).size(); i++)
            {
            
                if(al.get(0).size() > i)
                {
                    String s = Double.toString( numbers.get(i));
                    calcGUI.addStringToTextArea( s );
                }
                if(al.get(1).size() > i)
                {
                    String s = operations.get(i);
                    calcGUI.addStringToTextArea( s );
                }
            
            }
        
        }
        

        
    }
    

}
